﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BCMS.Business.Implementation;
using BCMS.FrontEnd.DataContract;
using BCMS.Settings;
using CrystalDecisions.Shared;

namespace BCMS.Forms.Report
{
    public partial class frmCouponReport : Form
    {
        public string ReportName { set; get; }
        public frmMain FrmParent { set; get; }

        public frmCouponReport()
        {
            InitializeComponent();
        }

        public frmCouponReport(string reportName, frmMain main)
        {
            this.Font = SystemFonts.MessageBoxFont;
            InitializeComponent();
            ReportName = reportName;
            FrmParent = main;
        }

        private void frmCouponByClaim_Load(object sender, EventArgs e)
        {
            switch (ReportName)
            {
                case "TotalClaim":
                    this.Text = "Báo cáo thu hồi theo người thu hồi";
                    dtReportDate.Enabled = false;
                    lblFromDate.Text = "Thu hồi từ ngày";
                    lblToDate.Text = "Thu hồi tới ngày";
                    cbClaimBy.Enabled = false;
                    cbStatus.Enabled = false;
                    break;
                case "ClaimByUser":
                    this.Text = "Báo cáo thu hồi theo người thu hồi";
                    dtReportDate.Enabled = false;
                    lblFromDate.Text = "Thu hồi từ ngày";
                    lblToDate.Text = "Thu hồi tới ngày";
                    lblClaimBy.Text = "Thu hồi bởi";
                    cbStatus.Enabled = false;
                    DALclient client = new DALclient();
                    List<UserUI> users = client.getAllUsers();
                    if (users != null)
                    {
                        AutoCompleteStringCollection autoSrc = new AutoCompleteStringCollection();
                        List<string> data = new List<string>();
                        foreach (UserUI str in users)
                        {
                            autoSrc.Add(str.UserName);
                            data.Add(str.UserName);
                        }

                        cbClaimBy.DataSource = data;
                        cbClaimBy.AutoCompleteCustomSource = autoSrc;
                        cbClaimBy.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

                    }
                    break;
                case "Expired":
                    this.Text = "Báo cáo phiếu hết hạn";
                    dtCpFrom.Enabled = false;
                    dtCpTo.Enabled = false;
                    cbClaimBy.Enabled = false;
                    cbStatus.Enabled = false;
                    break;
                case "UnExpired":
                    this.Text = "Báo cáo phiếu chưa hết hạn";
                    dtCpFrom.Enabled = false;
                    dtCpTo.Enabled = false;
                    cbClaimBy.Enabled = false;
                    cbStatus.Enabled = false;
                    break;
                case "ByReleaseDate":
                    this.Text = "Báo cáo phiếu theo ngày phát hành";
                    dtReportDate.Enabled = false;
                    lblFromDate.Text = "Phát hành từ ngày";
                    lblToDate.Text = "Phát hành tới ngày";
                    cbClaimBy.Enabled = false;
                    cbStatus.Enabled = false;
                    break;
                case "ExpireDetail":
                    this.Text = "Báo cáo phiếu hết hạn chi tiết";
                    //dtCpFrom.Enabled = false;
                    //dtCpTo.Enabled = false;
                    cbClaimBy.Enabled = false;
                    cbStatus.Enabled = false;
                    break;
                case "UnexpiredUnclaim":
                    this.Text = "Unexpired Unclaimed";
                    cbClaimBy.Enabled = false;
                    cbStatus.Enabled = false;
                    break; 
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            List<CouponBriefUI> src = new List<CouponBriefUI>();
            List<CouponUI> srcCp = new List<CouponUI>();
            DALclient client = new DALclient();

            switch (ReportName)
            {
                case "TotalClaim":
                    src = client.reportClaimedCoupons(dtCpFrom.Value.Date, dtCpTo.Value.Date);
                    break;
                case "ClaimByUser":
                    src = client.reportClaimedCouponsByUser(cbClaimBy.Text, dtCpFrom.Value.Date, dtCpTo.Value.Date);
                    break;
                case "Expired":
                    src = client.reportExpireCoupons(dtReportDate.Value.Date);
                    break;
                case "UnExpired":
                    src = client.reportUnExpireCoupons(dtReportDate.Value.Date);
                    break;
                case "ByReleaseDate":
                    src = client.reportCouponsByReleaseDate(dtCpFrom.Value.Date, dtCpTo.Value.Date);
                    break;
                case "ExpireDetail":
                    srcCp = client.reportExpiredCouponDetail(dtCpFrom.Value.Date, dtCpTo.Value.Date, dtReportDate.Value.Date);
                    break;
                case "UnexpireUnclaim":
                    srcCp = client.reportUnexpiredUnclaimed(dtCpFrom.Value.Date, dtCpTo.Value.Date, dtReportDate.Value.Date);
                    break;
                default:
                    src = null;
                    break;
            }

            if (src != null) //srcCP?????
            {
                if (ReportName.Equals("ExpireDetail") || ReportName.Equals("UnexpireUnclaim"))
                {
                    SortableBindingList<CouponUI> listCp = new SortableBindingList<CouponUI>(srcCp);
                    FrmParent.grvReport.DataSource = listCp;
                    FrmParent.formatCouponGrid(FrmParent.grvReport);
                    FrmParent.updateCouponsSummary(FrmParent.grvReport);
                }
                else
                {
                    SortableBindingList<CouponBriefUI> list = new SortableBindingList<CouponBriefUI>(src);
                    FrmParent.grvReport.DataSource = list;
                    FrmParent.formatCouponBrief(FrmParent.grvReport);
                    FrmParent.updateCouponBriefSummary(FrmParent.grvReport);
                }
            }
            else
                FrmParent.grvReport.DataSource = null;
            
            this.Close();
        }

        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            List<CouponBriefUI> src = new List<CouponBriefUI>();
            DALclient client = new DALclient();

            switch (ReportName)
            {
                case "TotalClaim":
                    src = client.reportClaimedCoupons(dtCpFrom.Value.Date, dtCpTo.Value.Date);

                    if (src != null)
                    {
                        rptCouponByClaim rptAcc = new rptCouponByClaim();
                        rptAcc.Load(Application.StartupPath + "\\Report\\rptCouponByClaim.rpt");
                        rptAcc.SetDataSource(src);

                        ParameterFields rParms = new ParameterFields();
                        ParameterField parm = new ParameterField();
                        ParameterDiscreteValue disValue = new ParameterDiscreteValue();
                        parm.Name = "FromDate";
                        disValue.Value = dtCpFrom.Value.Date;
                        parm.CurrentValues.Add(disValue);
                        rParms.Add(parm);

                        ParameterField parmTo = new ParameterField();
                        disValue = new ParameterDiscreteValue();
                        parmTo.Name = "ToDate";
                        disValue.Value = dtCpTo.Value.Date;
                        parmTo.CurrentValues.Add(disValue);
                        rParms.Add(parmTo);

                        frmReport report = new frmReport(rptAcc, rParms);
                        report.Show();
                    }
                    else
                        MessageBox.Show("Nothing to print", "Oops!");

                    break;
                case "ClaimByUser":
                    src = client.reportClaimedCouponsByUser(cbClaimBy.Text, dtCpFrom.Value.Date, dtCpTo.Value.Date);

                    if (src != null)
                    {
                        rptCouponClaimByUser rptAcc = new rptCouponClaimByUser();
                        rptAcc.Load(Application.StartupPath + "\\Report\\rptCouponClaimByUser.rpt");
                        rptAcc.SetDataSource(src);

                        ParameterFields rParms = new ParameterFields();
                        ParameterField parm = new ParameterField();
                        ParameterDiscreteValue disValue = new ParameterDiscreteValue();
                        parm.Name = "FromDate";
                        disValue.Value = dtCpFrom.Value.Date;
                        parm.CurrentValues.Add(disValue);
                        rParms.Add(parm);

                        ParameterField parmTo = new ParameterField();
                        disValue = new ParameterDiscreteValue();
                        parmTo.Name = "ToDate";
                        disValue.Value = dtCpTo.Value.Date;
                        parmTo.CurrentValues.Add(disValue);
                        rParms.Add(parmTo);

                        ParameterField claimBy = new ParameterField();
                        disValue = new ParameterDiscreteValue();
                        claimBy.Name = "ClaimBy";
                        disValue.Value = cbClaimBy.Text;
                        claimBy.CurrentValues.Add(disValue);
                        rParms.Add(claimBy);

                        frmReport report = new frmReport(rptAcc, rParms);
                        report.Show();
                    }
                    else
                        MessageBox.Show("Nothing to print", "Oops!");

                    break;
                case "Expired":
                    src = client.reportExpireCoupons(dtReportDate.Value.Date);
                    if (src != null)
                    {
                        rptCouponExpired rptAcc = new rptCouponExpired();
                        rptAcc.Load(Application.StartupPath + "\\Report\\rptCouponExpired.rpt");
                        rptAcc.SetDataSource(src);

                        ParameterFields rParms = new ParameterFields();
                        ParameterField parm = new ParameterField();
                        ParameterDiscreteValue disValue = new ParameterDiscreteValue();
                        parm.Name = "ReportDate";
                        disValue.Value = dtReportDate.Value.Date;
                        parm.CurrentValues.Add(disValue);
                        rParms.Add(parm);

                        frmReport report = new frmReport(rptAcc, rParms);
                        report.Show();
                    }
                    else
                        MessageBox.Show("Nothing to print", "Oops!");

                    break;
                case "UnExpired":
                    src = client.reportUnExpireCoupons(dtReportDate.Value.Date);
                    if (src != null)
                    {
                        rptCouponUnExpired rptAcc = new rptCouponUnExpired();
                        rptAcc.Load(Application.StartupPath + "\\Report\\rptCouponUnExpired.rpt");
                        rptAcc.SetDataSource(src);

                        ParameterFields rParms = new ParameterFields();
                        ParameterField parm = new ParameterField();
                        ParameterDiscreteValue disValue = new ParameterDiscreteValue();
                        parm.Name = "ReportDate";
                        disValue.Value = dtReportDate.Value.Date;
                        parm.CurrentValues.Add(disValue);
                        rParms.Add(parm);

                        frmReport report = new frmReport(rptAcc, rParms);
                        report.Show();
                    }
                    else
                        MessageBox.Show("Nothing to print", "Oops!");

                    break;
                case "ByReleaseDate":
                    src = client.reportCouponsByReleaseDate(dtCpFrom.Value.Date, dtCpTo.Value.Date);
                    if (src != null)
                    {
                        rptCouponByReleaseDate rptAcc = new rptCouponByReleaseDate();
                        rptAcc.Load(Application.StartupPath + "\\Report\\rptCouponByReleaseDate.rpt");
                        rptAcc.SetDataSource(src);

                        ParameterFields rParms = new ParameterFields();
                        ParameterField parm = new ParameterField();
                        ParameterDiscreteValue disValue = new ParameterDiscreteValue();
                        parm.Name = "FromDate";
                        disValue.Value = dtCpFrom.Value.Date;
                        parm.CurrentValues.Add(disValue);
                        rParms.Add(parm);

                        ParameterField parmTo = new ParameterField();
                        disValue = new ParameterDiscreteValue();
                        parmTo.Name = "ToDate";
                        disValue.Value = dtCpTo.Value.Date;
                        parmTo.CurrentValues.Add(disValue);
                        rParms.Add(parmTo);

                        frmReport report = new frmReport(rptAcc, rParms);
                        report.Show();
                    }
                    else
                        MessageBox.Show("Nothing to print", "Oops!");

                    break;
                case "ExpireDetail":
                    List<CouponUI> srcExpiredUnclaimed = client.reportExpiredCouponDetail(dtCpFrom.Value.Date, dtCpTo.Value.Date, dtReportDate.Value.Date);
                    if (srcExpiredUnclaimed != null)
                    {
                        List<cpDetailExport> srcCp = new List<cpDetailExport>();
                        foreach (CouponUI item in srcExpiredUnclaimed)
                            srcCp.Add(new cpDetailExport(item));

                        rptCouponExpireDetail rptAcc = new rptCouponExpireDetail();
                        rptAcc.Load(Application.StartupPath + "\\Report\\rptCouponExpireDetail.rpt");
                        rptAcc.SetDataSource(srcCp);

                        ParameterFields rParms = new ParameterFields();

                        //ParameterField parm = new ParameterField();
                        //ParameterDiscreteValue disValue = new ParameterDiscreteValue();
                        //parm.Name = "FromDate";
                        //disValue.Value = dtCpFrom.Value.Date;
                        //parm.CurrentValues.Add(disValue);
                        //rParms.Add(parm);

                        //ParameterField parm1 = new ParameterField();
                        //ParameterDiscreteValue disValue1 = new ParameterDiscreteValue();
                        //parm1.Name = "ToDate";
                        //disValue1.Value = dtCpTo.Value.Date;
                        //parm.CurrentValues.Add(disValue1);
                        //rParms.Add(parm1);


                        ParameterField parm2 = new ParameterField();
                        ParameterDiscreteValue disValue2 = new ParameterDiscreteValue();
                        parm2.Name = "ReportDate";
                        disValue2.Value = dtReportDate.Value.Date;
                        parm2.CurrentValues.Add(disValue2);
                        rParms.Add(parm2);

                        frmReport report = new frmReport(rptAcc, rParms);
                        report.Show();
                    }
                    else
                        MessageBox.Show("Nothing to print", "Oops!");

                    break;

                case "UnexpireUnclaim":
                    List<CouponUI> srcUnexpiredUnclaimed = client.reportUnexpiredUnclaimed(dtCpFrom.Value.Date, dtCpTo.Value.Date, dtReportDate.Value.Date);
                    if (srcUnexpiredUnclaimed != null)
                    {
                        List<cpDetailExport> srcCp = new List<cpDetailExport>();
                        foreach (CouponUI item in srcUnexpiredUnclaimed)
                            srcCp.Add(new cpDetailExport(item));

                        rptCouponExpireDetail rptAcc = new rptCouponExpireDetail();
                        rptAcc.Load(Application.StartupPath + "\\Report\\rptCouponUnexpiredUnclaimed.rpt");
                        rptAcc.SetDataSource(srcCp);

                        ParameterFields rParms = new ParameterFields();

                        ParameterField parm2 = new ParameterField();
                        ParameterDiscreteValue disValue2 = new ParameterDiscreteValue();
                        parm2.Name = "ReportDate";
                        disValue2.Value = dtReportDate.Value.Date;
                        parm2.CurrentValues.Add(disValue2);
                        rParms.Add(parm2);

                        frmReport report = new frmReport(rptAcc, rParms);
                        report.Show();
                    }
                    else
                        MessageBox.Show("Nothing to print", "Oops!");
                    break;
                default:
                    src = null;
                    break;
            }

        }

        private void cbClaimBy_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
